﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.Win.Controls;
using DevComponents.Editors;

namespace PlanningGIS.Raster
{
    /// <summary>
    ///作用：DEM数据入库
    ///作者：盛琦
    ///编写日期：2013-09-05
    /// </summary>
    public partial class FrmDomImp :  Office2007Form
    {
        public FrmDomImp()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "IMAGE(*.img)|*.img|TIF (*.tif)|*.tif";//"栅格文件(*.img,*.tif)|(*.img,*.tif)";
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtInput.Text = openFileDialog.FileName;

                string fileName = System.IO.Path.GetFileNameWithoutExtension(openFileDialog.FileName);
                txtName.Text = fileName;
            }
        }

        private void btnImp_Click(object sender, EventArgs e)
        {
            if (!RasterHelper.DbConfig.Context.ExistsObject("md_cq_dem"))
            {
                MsgHelper.ShowWarningMsgbox("数据库中缺少测区级DEM元数据表，请检查！");
                return;
            }

            if (txtInput.Text == "")
            {
                MsgHelper.ShowWarningMsgbox("请先选择需要入库的栅格数据！");
                return;
            }

            DomMetaData dom = new DomMetaData();
            dom.ID = Guid.NewGuid().ToString();


            string year = DateTime.Now.Year.ToString();
            string month = DateTime.Now.Month.ToString();

            if (month.Length == 1) month = "0" + month;

            string folder = RasterHelper.FTP_TYPE + @"\DOM\" + year + month;
            dom.Folder = folder;

            double? resolution = null;
            if (txtResolution.Text != "") resolution = Double.Parse(txtResolution.Text);
            dom.Resolution = resolution;

            string color = "";
            switch (cboColor.SelectedIndex)
            {
                case 0:
                    color = "COLOR";
                    break;

                case 1:
                    color = "BLACK";
                    break;
            }

            dom.Color = color;


            string rsType = "";
            switch (cboRsType.SelectedIndex)
            {
                case 0:
                    rsType = "AR";
                    break;

                case 1 :
                    rsType = "RS";
                    break;
            }

            dom.RsType = rsType;

            dom.Name = txtName.Text;
            dom.Date = dateTimePicker1.Value;
            dom.Format = System.IO.Path.GetExtension(txtInput.Text);

            this.Cursor = Cursors.WaitCursor;

            try
            {
                RasterHelper.UploadDom(txtInput.Text, dom);

                MsgHelper.ShowInfomationMsgbox("DOM数据入库完成。");
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox("DOM数据入库失败：" + ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void FrmDomImp_Load(object sender, EventArgs e)
        {
            txtResolution.KeyPress += ControlVerify.txtDoubleBox_KeyPress;
            cboColor.SelectedIndex = 0;
            cboRsType.SelectedIndex = 0;

            dateTimePicker1.Format = eDateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "yyyy-MM-dd";
            dateTimePicker1.Value = DateTime.Now;
        }
    }
}
